<?php
namespace Home\Action;
use Think\Page;
class UserAction extends BaseAction {
    public function _initialize() {
  parent::_initialize();
  if($this->visitor->is_login == false){
        	   $url=U('login/index','','');
           redirect($url);
    }
        $this->modorder=M('order');
		$this->artmod=M('article');
		$user=$this->visitor->get();
	   $this->assign('user',$user);
//		if($user['webmaster'] == 1){
//			$this->redirect('zhan/ucenter');
//		}

		
		if($user['tbname'] == 1){
		  $this->assign('isagent',true);
		}

	   
  }
  
public function suborder(){
$mod=M('user');
$this->_config_seo(array(
			'title'=>'提交淘宝订单'
			));  
if(IS_POST){
$orderid = I('orderid','','trim');
if(is_numeric($orderid)){

$UID=$this->visitor->get('id');

$map=array(
'id'=>$UID,
);
$oid=md5(substr($orderid,-6,6));
$data=array(
'oid'=>$oid
);

$isset=$mod->field('id')->where($data)->find();

if(!$isset){
	
$res=$mod->where($map)->save($data);
if($res){
return $this->ajaxReturn(1,'提交成功！');
}else{
$this->ajaxReturn(0,'提交失败，请检查后重试');	
}

}else{
$this->ajaxReturn(0,'此订单已经被其它账号绑定过！');		
}

}else{
$this->ajaxReturn(0,'提交的订单参数不符合要求');
	
}

}
			

$oid=$this->visitor->get('oid');	
if($oid){
$this->assign('oid',true);
}
			
			
		$this->display();	

	}
	
	
public function myorder(){
	
	    $p = I('p', 1, 'intval');
	    $status = I('status', 0, 'intval');
		$page_size = 10;
		$start = $page_size * ($p - 1);
		if($status){
		$stay['status'] = $status;
		$this->assign('status',$status);
		}
		
		$stay['uid'] = $this->memberinfo['id'];
		$stay['nstatus'] = 1;
		$rows = $this->modorder->where($stay)->order('id desc')->limit($start . ',' . $page_size)->select();
		$count = $this->modorder->where($stay)->count();
		$pager = new Page($count, $page_size);
		$this->assign('page', $pager->show());
		$this->assign('total_item', $count);
		$this -> assign('page_size',$page_size);
		$list=array();
		$fuid=$this->memberinfo['fuid'];
		$guid=$this->memberinfo['guid'];
		foreach($rows as $k=>$v){
			$list[$k]['status']=$this->orderstatic($v['status']);
			$list[$k]['state']=$v['status'];
			$list[$k]['orderid']=$v['orderid'];
			$list[$k]['add_time']=$v['add_time'];
			$list[$k]['price']=$v['price'];
			$list[$k]['income']=Orebate1(array('price'=>$v['income'],'leve'=>$v['leve1']));
			$list[$k]['up_time']=$v['up_time'];
			$list[$k]['id']=$v['id'];
		}
		
		$this->assign('list',$list);
		
	
$this->_config_seo(array(
			'title'=>'我的订单'
			)); 
$this->display();	
}
 
public function teamorder(){
$p = I('p', 1, 'intval');
$uid=$this->memberinfo['id'];
$fuid = I('fuid', 0, 'intval');
$guid = I('guid', 0, 'intval');
$page_size = 10;
$start = $page_size * ($p - 1);

if($fuid){
$stay['fuid'] = $uid;	
$this->assign('fuid',$fuid);
}

if($guid){
$stay['guid'] = $uid;	
$this->assign('guid',$guid);
}

if(!$guid && !$fuid){
$stay['fuid'] = $uid;	
$this->assign('fuid',1);	
}
$stay['nstatus'] = 1;
$rows = $this->modorder->field('status,orderid,add_time,price,income,up_time,id,leve1,leve2,leve3')->where($stay)->order('id desc')->limit($start . ',' . $page_size)->select();
//
$count = $this->modorder->where($stay)->count();

$pager = new Page($count, $page_size);
$this->assign('page', $pager->show());
$this->assign('total_item', $count);
$this -> assign('page_size',$page_size);

$list=array();
		foreach($rows as $k=>$v){
			$list[$k]['status']=$this->orderstatic($v['status']);
			$list[$k]['state']=$v['status'];
			$list[$k]['orderid']=$v['orderid'];
			$list[$k]['add_time']=$v['add_time'];
			$list[$k]['price']=$v['price'];
			$list[$k]['income']=$v['income'];
			$list[$k]['up_time']=$v['up_time'];
			$list[$k]['id']=$v['id'];
			$list[$k]['leve1']=$v['leve1'];
			$list[$k]['leve2']=$v['leve2'];
			$list[$k]['leve3']=$v['leve3'];
		}
		
		$this->assign('list',$list);
	
$this->_config_seo(array(
			'title'=>'团队订单'
			)); 
		
$this->display();		
}
  
  
public function myteam(){
$usermod=M('user');
$uid=$this->memberinfo['id'];
$today_str = mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
$last_str = mktime(0, 0, 0, date("m"), date("d")-2, date("Y"));
$end =date('Y-m-01', strtotime(date("Y-m-d")));
$end = strtotime($end);
        $field = 'id,
        ( select count(id) from tqk_user where fuid = '.$uid.' or guid = '.$uid.') as allperson,
        ( select count(id) from tqk_user where (fuid = '.$uid.' or guid = '.$uid.') and reg_time>'.$today_str.' ) as todayperson,
        ( select count(id) from tqk_user where (fuid = '.$uid.' or guid = '.$uid.') and (reg_time>'.$last_str.' and reg_time<'.$today_str.') ) as lastperson,
        	( select count(id) from tqk_user where (fuid = '.$uid.' or guid = '.$uid.') and reg_time>'.$end.' ) as thismonthperson,
        ( select count(id) from tqk_user where fuid = '.$uid.') as person1,
        ( select count(id) from tqk_user where guid = '.$uid.') as person2';
        $res = $usermod->field($field)->find();
        $this->assign('stat',$res);

$p = I('p', 1, 'intval');
$fuid = I('fuid', 0, 'intval');
$guid = I('guid', 0, 'intval');
$page_size = 10;
$start = $page_size * ($p - 1);

if($fuid){
$stay['fuid'] = $uid;	
$this->assign('fuid',$fuid);
}

if($guid){
$stay['guid'] = $uid;	
$this->assign('guid',$guid);
}

if(!$guid && !$fuid){
$stay['fuid'] = $uid;	
$this->assign('fuid',1);	
}

$rows = $usermod->field('reg_time,nickname,oid')->where($stay)->order('id desc')->limit($start . ',' . $page_size)->select();
$count = $usermod->where($stay)->count();
$pager = new Page($count, $page_size);
$this->assign('page', $pager->show());
$this->assign('total_item', $count);
$this->assign('info',$rows);
$this -> assign('page_size',$page_size);


	
$this->_config_seo(array(
			'title'=>'我的团队'
			)); 
		
$this->display();	
	
}


public function teamprofit(){
$modfin=M('finance');
$uid=$this->memberinfo['id'];
$fuid=$this->memberinfo['fuid'];
$guid=$this->memberinfo['guid'];
$today_str = mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
$tomorr_str = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$pre_time=mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$last_str = mktime(0, 0, 0, date("m"), date("d")-2, date("Y"));
$pre_month=$this->getthemonth($pre_time);

$field = 'id,
( select SUM(income) from tqk_finance where uid = '.$uid.' and type=3) as allmoney,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and uid = '.$uid.' and (add_time>'.$last_str.' and add_time<'.$today_str.') ) as u_lastmoney,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and fuid = '.$uid.' and (add_time>'.$last_str.' and add_time<'.$today_str.') ) as f_lastmoney,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and guid = '.$uid.' and (add_time>'.$last_str.' and add_time<'.$today_str.') ) as g_lastmoney,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and uid = '.$uid.' and add_time>'.$pre_month[1].') as u_month,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and fuid = '.$uid.' and add_time>'.$pre_month[1].') as f_month,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and guid = '.$uid.' and add_time>'.$pre_month[1].') as g_month,
( select count(id) from tqk_order where nstatus=1 and (status =1 or status =3) and (uid = '.$uid.' or fuid='.$uid.' or guid='.$uid.') and add_time>'.$today_str.') as today_count,
( select count(id) from tqk_order where nstatus=1 and (status =1 or status =3) and (uid = '.$uid.' or fuid='.$uid.' or guid='.$uid.') and add_time>'.$pre_month[1].' ) as this_month_count,
( select count(id) from tqk_order where nstatus=1 and (status =1 or status =3) and (uid = '.$uid.' or fuid='.$uid.' or guid='.$uid.') and (add_time>'.$pre_month[0].' and add_time<'.$pre_month[1].') ) as last_month_count';
$res = $modfin->field($field)->find();
if($res){

$allmoney=$res['allmoney'];

$this->assign('allmoney',$allmoney);
	
	
$yesterday_money=Rebate1($res['u_lastmoney'])+Rebate2($res['f_lastmoney'])+Rebate3($res['g_lastmoney']);
$this->assign('yesterday_money',$yesterday_money);

$this_month=Rebate1($res['u_month'])+Rebate2($res['f_month'])+Rebate3($res['g_month']);
$this->assign('this_month',$this_month);

	
}

$this->assign('stat',$res);

$p = I('p', 1, 'intval');
$page_size = 6;
$start = $page_size * ($p - 1);
$stay['uid'] = $uid;
$stay['type'] = 3;
$rows = $modfin->where($stay)->order('id desc')->limit($start . ',' . $page_size)->select();
		$count = $modfin->where($stay)->count();
		$pager = new Page($count, $page_size);
		$this->assign('page', $pager->show());
		$this->assign('total_item', $count);
		$this -> assign('page_size',$page_size);
		$list=array();
		foreach($rows as $k=>$v){
			$list[$k]['status']=$this->Fstatic($v['status']);
			$list[$k]['mark']=$v['mark'];
			$list[$k]['price']=$v['price'];
			$list[$k]['add_time']=$v['add_time'];
			$list[$k]['backcash']=$v['backcash'];
			$list[$k]['income']=$v['income'];
			$list[$k]['id']=$v['id'];
		}

$this->assign('list',$list);


$this->_config_seo(array(
			'title'=>'我的收益'
			)); 
$this->display();

}


public function vieworder(){
 $this->_config_seo(array(
            'title' => '帮助中心'
        ));
 $helps =M('help')->field('id,title')->select();
   $this->assign('helps', $helps);
$this->display();
	
}

public function bindtb(){
$time=NOW_TIME;
$map=array(
'time'=>$time,
'tqk_uid'=>$this->tqkuid,
'uid'=>$this->visitor->get('id'),
'web'=>'pc'
);
$token=$this->create_token(trim(C('yh_gongju')),$map);
$url='http://api.tuiquanke.cn/bindtaobao?time='.$time.'&tqk_uid='.$this->tqkuid.'&uid='.$this->visitor->get('id').'&web=pc&token='.$token;
echo('<script>window.location.href="'.$url.'" </script>');
}

public function fillphone(){
 if(IS_POST){
  $phone = I('phone','','trim');

$exist = M('user')->where(array(
            'phone' => $phone
 ))->count('id');
 
  if ($exist) {
 return $this->ajaxReturn(0, '手机号已存在！');
        }

$data['phone'] = $phone;
$res=M('user')->where('id='.$this->visitor->get('id'))->save($data);
if($res){
return $this->ajaxReturn(1, '成功保存！');
}else{
return $this->ajaxReturn(0, '保存失败！');
	
}
	
}

$this->display();	
	
}

public function ucenter(){
 $article=$this->artmod->where('cate_id=2')->field('id,title')->limit(5)->select();
 $this->assign('article',$article);	
 $uid=$this->memberinfo['id'];
 
 if(!$uid){
		$url=U('login/index','','');
		redirect($url);
	}
 
 
if($uid>0){
$code=$this->visitor->get('invocode');
if(!$code){
		$codes=$this->invicode($uid);
		$this->assign('invicode',$codes);
}

}


		$tbname=$this->visitor->get('tbname');
		if(1 != $tbname){
		$map=array(
		'uid'=>$uid,
		'nstatus'=>1,
		'status'=>3
		);
		$mymoney=$this->modorder->where($map)->sum('price');
		$agentcondition=trim(C('yh_agentcondition'));
		if($mymoney>=$agentcondition){
		M('user')->where(array('id'=>$uid))->setField('tbname',1);	
		}
			
		}

 
 $where=array(
 'oid'=>$this->visitor->get('oid'),
 'uid'=> $uid,
 'status'=>1,
 );
 
 $integral=$this->modorder->where($where)->sum('integral');
 $this->assign('integral',$integral?$integral:0);
$stay['oid'] = $this->visitor->get('oid');
$stay['uid'] = $this->memberinfo['id'];
$rows = $this->modorder->where($stay)->order('id desc')->limit(10)->select();
 $list=array();
		foreach($rows as $k=>$v){
		$list[$k]['status']=$this->orderstatic($v['status']);
		$list[$k]['orderid']=$v['orderid'];
		$list[$k]['add_time']=$v['add_time'];
		$list[$k]['price']='￥'.$v['price'];
		$list[$k]['integral']='￥'.Orebate1(array('price'=>$v['income'],'leve'=>$v['leve1']));
		$list[$k]['up_time']=$v['up_time'];
		}
	
$this->assign('list',$list);
 
 
 $this->_config_seo(array(
            'title'=>'用户中心'
        )); 
$this->display();
}

protected function orderstatic($id){
switch($id){
	case 0 :
	return '待处理';
	break;
	case 1 :
	return '已付款';
	break;
	case 2 :
	return '无效订单';
	break;
	case 3 :
	return '已结算';
	break;
	default : 
	return '订单失效';
	break;
}
	
}



public function order(){
	
$p = I('p', 1, 'intval');
		$page_size = 10;
		$start = $page_size * ($p - 1);
		$stay['oid'] = $this->visitor->get('oid');
		$stay['uid'] = $this->memberinfo['id'];	
		$stay['nstatus'] = 0;	
		$rows = $this->modorder->where($stay)->order('id desc')->limit($start . ',' . $page_size)->select();
		$count = $this->modorder->where($stay)->count();
		$pager = new Page($count, $size);
        $this->assign('page', $pager->show());
        $this->assign('total_item', $count);
        $this -> assign('page_size',$page_size);
		$list=array();
		foreach($rows as $k=>$v){
		$list[$k]['status']=$this->orderstatic($v['status']);
		$list[$k]['orderid']=$v['orderid'];
		$list[$k]['state']=$v['status'];
		$list[$k]['add_time']=$v['add_time'];
		$list[$k]['price']="￥".$v['price'];
		$list[$k]['integral']=$v['integral'];
		$list[$k]['up_time']=$v['up_time'];
		$list[$k]['id']=$v['id'];
		}
	
	$this->assign('list',$list);
	
$where=array(
 'oid'=>$this->visitor->get('oid'),
 'uid'=>$this->memberinfo['id'],
 'status'=>1,
  'nstatus'=>0
 );
 
 $integral=$this->modorder->where($where)->sum('integral');
 
 $this->assign('integral',$integral?$integral:0);
	
	 $this->_config_seo(array(
            'title'=>'我的订单'
        )); 
		
$this->display();	
	
	
}


public function jifen(){

if(IS_POST){
$count = abs(I('count','','trim'));
if ($count<=0) {
$this->ajaxReturn(0, '兑换数量与实际数量不符');
}

$user=M('user')->where('id='.$this->memberinfo['id'])->field('id,score,money')->find();

if ($count>$user['score']) {
$this->ajaxReturn(0, '兑换数量与实际数量不符');
}

$userid=$this->memberinfo['id'];
$price=(C('yh_fanxian')/100)*$count;

if($price>0){
	
M('user')->where('id='.$userid)->save(array(
 'money'=>array('exp','money+'.$price),
 'score'=>array('exp','score-'.$count)
));

 M('usercash')->add(array(
	    		'uid'         =>$userid,
	    		'money'       =>$price,
	    		'remark'      =>'积分兑换: '.$price.'元',
	    		'type'        =>10, 
	    		'create_time' =>time(),
	    		'status'      =>1,
	    	));
$this->ajaxReturn(1, '兑换成功！');

}else{
	
	$this->ajaxReturn(0, '兑换失败！');
}
	
}



 $this->_config_seo(array(
            'title'=>'积分兑换'
        )); 
		
$this->display();	
	
}



     
     
public function index() {
      	$callback=I('callback', '', 'trim');     
		$where['openid'] = $this->openid;
		$user = M('user')->where($where)->field('nickname,avatar,money,score,openid')->find();
		if($user){
			$this->assign('user',$user);				
		}else{
			exit('您的账号不存在！');
		}
	    $this->_config_seo(array(
            'title'=>'用户中心'
        ));                         
      	$this->display();
	}
	
	public function modify() {
		if(IS_POST){
			$password = I('password','','trim');
			$password2 = I('password2','','trim');
			$data=array(
				'nickname'=>I('nickname','','trim'),
//				'username'=>$this->_param('username','trim'),
				'qq'=>I('qq','','trim'),
				'wechat'=>I('wechat','','trim'),
			);
			if(I('avatar','','trim')){
				$data['avatar'] = I('avatar','','trim');
			}
			if($password){
				if($password == $password2){
					$data['password'] = md5($password);
				}else{
					$this->ajaxReturn(0,'两次密码不一致');
				}
			}
			
			if($_FILES['avatar']){
	            $file = $this->_upload($_FILES['avatar'], 'avatar',$thumb = array('width'=>150,'height'=>150));
	            if($file['error']) {
	            	$this->ajaxReturn(0,$file['info']);
	            } else {
	             $data['avatar']=$file['mini_pic'];
	            }
	   		}
			
			$F=M('user');
			$where['id'] = $this->visitor->get('id');
			$res = $F->where($where)->save($data);
			
			if($res !== false){
		  		return $this->ajaxReturn(1,'修改成功');
		    }
			$this->ajaxReturn(0,'修改失败');
		}
		$F=M('user');
		$where['id'] = $this->visitor->get('id');
		$info = $F->where($where)->field('nickname,avatar,username,qq,wechat,phone')->find();
		$this->assign('info',$info);
		$this->_config_seo(array(
            'title'=>'修改资料'
        )); 
		$this->display();
	}
	
	public function tixian() {
		if(IS_POST){
			$F=M('balance');
			$mymoney = abs(I('money','','trim'));
			if($mymoney<=0){
			$this->ajaxReturn(0,'提现金额异常！');
				exit();	
			}
			$map['id'] = $this->visitor->get('id');
			$balance = M('user')->field('nickname,avatar,username,money,id')->where($map)->find();
     		if($mymoney>$balance['money']){
     			$this->ajaxReturn(0,'账户余额不足！');
				exit();
     		}
     			$data=array(
					'uid'=>$balance['id'],
					'money'=>$mymoney,
					'name'=>I('name','','trim'),
					'method'=>I('method','','trim'),
					'allpay'=>I('allpay','','trim'),
					'status'=>0,
					'content'=>I('content','','trim'),
					'create_time'=>time()
				);
			$res = $F->add($data);
			if($res !== false){
			M('user')->where(array(
                'id'=>$balance['id']
            ))->save(array(
                'money'=>array('exp','money-'.$mymoney),
                'frozen'=>array('exp','frozen+'.$mymoney),
));

M('usercash')->add(array(
                'uid'=>$balance['id'],
                'money'=>$mymoney,
                'type'=>1,
                'remark'=>'提现冻结资金：'.$mymoney.'元',
                'create_time'=>NOW_TIME,
                'status'=>1,
   ));
					
			  return $this->ajaxReturn(1,'申请提交成功，请等待处理！');
			    }
				$this->ajaxReturn(0,'申请提交失败！');
		}
		 
	$where['id'] = $this->visitor->get('id');
    	$user = M('user')->where($where)->field('nickname,avatar,username,money')->find();
    	$this->assign('user',$user);
	    $this->_config_seo(array(
            'title'=>'我要提现'
        ));                         
      	$this->display();
	}
	
	public function journal() {
		$p = I('p', 1, 'intval');
		$page_size = 10;
		$start = $page_size * ($p - 1);
		$where['id'] = $this->visitor->get('id');
		$user = M('user')->where($where)->field('nickname,avatar,username,money,frozen,id')->find();
		if($user){
			$this->assign('user',$user);				
		}else{
			exit('您的账号不存在！');
		}
		
		$where=array(
			'type'=>6,
			'uid'=>$user['id']
		);
		$balance = M('usercash')->where($where)->sum('money');
		if($balance){
			$this->assign('balance',$balance);		
		}else{
			$this->assign('balance',0);		
		}
		$stay['uid'] = $user['id'];
		$rows = M('usercash')->where($stay)->field('type,money,create_time,status')->order('id desc')->limit($start . ',' . $page_size)->select();
		$count = M('usercash')->where($stay)->count();
		$pager = new Page($count, $page_size);
        $this->assign('page', $pager->show());
        $this->assign('total_item', $count);
        $this -> assign('page_size',$page_size);
		$list=array();
		foreach($rows as $k=>$v){
		$val=unserialize(user_cash_type($v['type']));
		$list[$k]['create_time']=$v['create_time'];
		$list[$k]['type']=$val[0];
		$list[$k]['money']=$val[1].$v['money'];
		}
       $this->assign('info',$list);
		$this->_config_seo(array(
            'title'=>'财务日志'
        )); 
		$this->display();
	}
	
	public function record() {
		$p = I('p', 1, 'intval');
		$page_size = 10;
		$start = $page_size * ($p - 1);
		$where['id'] = $this->visitor->get('id');
		$user = M('user')->where($where)->field('frozen,nickname,avatar,money,score,openid')->find();
		if($user){
			$this->assign('user',$user);				
		}else{
			exit('您的账号不存在！');
		}
      	$map['uid'] = $this->visitor->get('id');
		$rows = M('balance')->where($map)->field('money,create_time,status')->limit($start . ',' . $page_size)->order('id desc')->select();
		$count = M('balance')->where($map)->count();
		$pager = new Page($count, $page_size);
        $this->assign('page', $pager->show());
        $this->assign('total_item', $count);
        $this -> assign('page_size',$page_size);
		$this->assign('info',$rows);
		$this->_config_seo(array(
            'title'=>'我的钱包'
        )); 
		$this->display();
	}
}

